<!-- 某个合同绑定的水表列表查看 | 解表 -->
<template>
	<div class="zs-table-page-container">
		<div class="zs-card-sm zs-space-bottom zs-layout-flex-row zs-layout-justify-between">
			<span>
				<el-input v-model="queryParams.assetsPromoteNum" class="zs-select-md zs-space-right-sm" placeholder="编号" disabled />
				<el-input v-model="queryParams.brandName" class="zs-select-md zs-space-right-sm" placeholder="品牌名称" disabled />
			</span>
		</div>

		<!-- teble -->
		<div class="zs-card zs-table-page-content">
			<el-table class="zs-table-page-main" :data="tableData.data" v-loading="tableData.loading" stripe>
				<el-table-column type="index" label="序号" width="60" align="center" :index="(index) => index + 1 + (tableData.currentPage - 1) * curPageSize" />
				<el-table-column prop="waterMeterCode" label="水表编号" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="waterMeterName" label="水表名称" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="floorNum" label="所在楼层" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="deviceLocation" label="所在位置" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="waterMeterTypeTxt" label="表类型" minWidth="120" align="center" :formatter="defaultTableData" />
				<el-table-column prop="waterMeterModelTxt" label="表种类" minWidth="120" align="center" :formatter="defaultTableData" />
				<el-table-column prop="multiplier" label="倍率" minWidth="120" align="center" :formatter="defaultTableData" />
				<el-table-column prop="coefficient" label="系数" minWidth="120" align="center" :formatter="defaultTableData" />
				<el-table-column v-if="route.query.type == 'break'" label="操作" width="90" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'break')" v-btnPermi="{ permis: ['p_btn_property_inspection_list_view'] }">解表</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="zs-space-top-sm zs-table-page-pagination">
				<el-pagination v-model:currentPage="tableData.currentPage" v-model:page-size="curPageSize" :page-sizes="curPageSizes" :small="true" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="pageChange('size', $event)" @current-change="pageChange('page', $event)" />
			</div>
		</div>

		<!-- 解表 -->
		<DrawerWaterBreak :isShow="drawer.break.isShowDrawer" :isReadOnly="drawer.break.isReadOnly" :handleType="drawer.break.handleType" :currentRow="currentRow.data" :contractInfo="contractInfo.data" @close="closeDrawer($event, 'break')"> </DrawerWaterBreak>
	</div>
</template>

<script setup>
	import DrawerWaterBreak from '../components/drawerWaterBreak.vue';
	import { waterMeterSeeApi, waterMeterBreakListApi, contractInfoApi } from '@/api/energy.js';
	import { ref, reactive } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute } from 'vue-router';
	import { defaultTableData, getDicName } from '@/utils/publicUtils';

	const route = useRoute();
	const store = useStore();
	const curPageSizes = ref(store.state.uiPageSizes);
	const curPageSize = ref(store.state.uiPageSizeDefault);

	// 列表 filed
	let tableData = reactive({
		currentPage: 1,
		total: 0,
		loading: false,
		data: [],
	});

	//查询条件
	let queryParams = reactive({ contractId: route.query.id, pageNumber: 1, pageSize: curPageSize.value });

	// 抽屉相关
	let currentRow = reactive({ data: {} });
	let drawer = reactive({
		break: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
	});

	const openDrawer = (flag, handleType, row, drawerType) => {
		drawer[drawerType].isReadOnly = flag;
		drawer[drawerType].isShowDrawer = true;
		drawer[drawerType].handleType = handleType;
		currentRow.data = row;
	};

	const closeDrawer = (data, drawerType) => {
		drawer[drawerType].isShowDrawer = data.isShow;
		data.isRefresh && queryList('init');
	};

	// 列表数据源
	const queryList = async init => {
		if (process.env.NODE_ENV !== 'development') {
			let portalResourceList = JSON.parse(localStorage.getItem('portalResourceList'));
			if (init != 'init' && !portalResourceList.includes('p_btn_property_inspection_list_query')) return;
		}
		tableData.loading = true;
		let res = route.query.type == 'break' ? await waterMeterBreakListApi(queryParams) : await waterMeterSeeApi(queryParams);
		if (res.data.code !== 200) return;
		initData(res.data.data.list);
		tableData.data = res.data.data.list || [];
		tableData.total = res.data.data.total || 0;
		tableData.loading = false;
	};

	const initData = list => {
		list.forEach(item => {
			item.waterMeterTypeTxt = item.waterMeterType == 1 ? '预付费' : item.waterMeterType == 2 ? '后付费' : '-';
			item.waterMeterModelTxt = item.waterMeterModel == 1 ? '普通能源表' : item.waterMeterType == 2 ? '智能表' : '-';
		});
	};

	// 分页
	const pageChange = (type, size) => {
		if (type == 'size') {
			queryParams.pageSize = size;
		} else {
			queryParams.pageNumber = size;
		}
		queryList('init');
	};

	//合同详情
	let contractInfo = reactive({ data: {} });
	const getContractInfo = async() => {
		let res = await contractInfoApi({contractId: route.query.id})
		contractInfo.data = res.data.data
		queryParams.brandName = res.data.data.brandName
		queryParams.assetsPromoteNum = res.data.data.assetsPromoteNum
	}

	queryList('init');
	getContractInfo();
</script>
